package com.ruoyi.erp.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.domain.Project;
import com.ruoyi.erp.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.StaffingSettingMapper;
import com.ruoyi.erp.domain.StaffingSetting;
import com.ruoyi.erp.service.IStaffingSettingService;
import com.ruoyi.common.core.text.Convert;

/**
 * 外派属性Service业务层处理
 * 
 * @author sauger
 * @date 2022-03-07
 */
@Service
public class StaffingSettingServiceImpl implements IStaffingSettingService 
{
    @Autowired
    private StaffingSettingMapper staffingSettingMapper;

    @Autowired
    private IProjectService projectService;
    /**
     * 查询外派属性
     * 
     * @param id 外派属性主键
     * @return 外派属性
     */
    @Override
    public StaffingSetting selectStaffingSettingById(Integer id)
    {
        return staffingSettingMapper.selectStaffingSettingById(id);
    }

    @Override
    public StaffingSetting selectStaffingSettingByResouce(String resourceType, Integer resourceId) {
        StaffingSetting search = new StaffingSetting();
        search.setResourceType(resourceType);
        search.setResourceId(resourceId);
        List<StaffingSetting> settings = selectStaffingSettingList(search);

        return settings.isEmpty() ? null : settings.get(0);
    }

    @Override
    public StaffingSetting selectStaffingSettingByResourceWithDefault(String resourceType, Integer resourceId) {
        StaffingSetting exist = selectStaffingSettingByResouce(resourceType, resourceId);
        if (exist != null) {
            return  exist;
        }
        StaffingSetting result;
        if (resourceType.equals("project"))
        {
            //返回项目对应客户的默认值
            Project project = projectService.selectProjectById(resourceId.intValue());
            if (project == null || (project.getCustomerId() != null && project.getCustomerId()<=0))
            {
                result =  selectSystemDefaultSetting();
            }else{
                result = selectStaffingSettingByResourceWithDefault("customer", project.getCustomerId());
            }
        }else {
            result = selectSystemDefaultSetting();
        }
        result.setResourceId(resourceId);
        result.setResourceType(resourceType);
        return result;
    }

    private StaffingSetting selectSystemDefaultSetting()
    {
        StaffingSetting setting = new StaffingSetting();
//        setting.setResourceId(resourceId);
//        setting.setResourceType(resourceType);
        setting.setExpenseRate(BigDecimal.valueOf(115));
        setting.setTax(BigDecimal.valueOf(6));
        setting.setBillFeeRate(BigDecimal.valueOf(6));
        return setting;
    }

    /**
     * 查询外派属性列表
     * 
     * @param staffingSetting 外派属性
     * @return 外派属性
     */
    @Override
    public List<StaffingSetting> selectStaffingSettingList(StaffingSetting staffingSetting)
    {
        return staffingSettingMapper.selectStaffingSettingList(staffingSetting);
    }

    /**
     * 新增外派属性
     * 
     * @param staffingSetting 外派属性
     * @return 结果
     */
    @Override
    public int insertStaffingSetting(StaffingSetting staffingSetting)
    {
        staffingSetting.setCreateTime(DateUtils.getNowDate());
        this.calculateQuotation(staffingSetting);
        return staffingSettingMapper.insertStaffingSetting(staffingSetting);
    }

    /**
     * 修改外派属性
     * 
     * @param staffingSetting 外派属性
     * @return 结果
     */
    @Override
    public int updateStaffingSetting(StaffingSetting staffingSetting)
    {
        staffingSetting.setUpdateTime(DateUtils.getNowDate());
        this.calculateQuotation(staffingSetting);
        return staffingSettingMapper.updateStaffingSetting(staffingSetting);
    }

    /**
     * 批量删除外派属性
     * 
     * @param ids 需要删除的外派属性主键
     * @return 结果
     */
    @Override
    public int deleteStaffingSettingByIds(String ids)
    {
        return staffingSettingMapper.deleteStaffingSettingByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除外派属性信息
     * 
     * @param id 外派属性主键
     * @return 结果
     */
    @Override
    public int deleteStaffingSettingById(Integer id)
    {
        return staffingSettingMapper.deleteStaffingSettingById(id);
    }

    private void calculateQuotation(StaffingSetting staffingSetting)
    {
        if (StringUtils.isNull(staffingSetting.getQuotation())) return;
        switch (staffingSetting.getQuotationType())
        {
            case "M":
                staffingSetting.setQuotationMonth(staffingSetting.getQuotation());
                staffingSetting.setQuotationDay(staffingSetting.getQuotation().divide(BigDecimal.valueOf(21.75),2,BigDecimal.ROUND_HALF_UP));
                break;
            case "D":
                staffingSetting.setQuotationMonth(staffingSetting.getQuotation().multiply(BigDecimal.valueOf(21.75)));
                staffingSetting.setQuotationDay(staffingSetting.getQuotation());
                break;
            case "H":
                staffingSetting.setQuotationMonth(staffingSetting.getQuotation().multiply(BigDecimal.valueOf(21.75*8)));
                staffingSetting.setQuotationDay(staffingSetting.getQuotation());
                break;
            default:
        }
    }
}
